<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Swoole\Async\Client as swoole_client;
class SwooleClient extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'client:swoole';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $client = new swoole_client(SWOOLE_SOCK_TCP,SWOOLE_SOCK_ASYNC);
        $client->on("connect",function(swoole_client $cli){
            $cli->send("GET / HTTP/1.1\r\n\r\n");
        });
        $client->on("receive",function(swoole_client $cli,$data){
            echo "Receive $data";
            $cli->send(str_repeat('A',100)."\n");
            sleep(1);
        });
        $client->on("error",function(swoole_client $cli){
            echo "error \n";
        });
        $client->on("close",function(swoole_client $cli){
            echo "Connection close \n";
        });

        $client->connect('127.0.0.1',8089);
    }
}
